package com.hou.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hou.commonutils.R;
import com.hou.educms.entity.CrmBanner;
import com.hou.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-14
 * 后台banner控制器
 */
@RestController
@RequestMapping("/educms/banner/admin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService service;

    // 分页查询banner
    @GetMapping("pageBanner/{page}/{limt}")
    public R pageBanner (@PathVariable long page, @PathVariable long limt) {

        Page<CrmBanner> bannerPage = new Page<>(page,limt);
        service.page(bannerPage,null);

        return R.ok().data("itmes",bannerPage.getRecords()).data("total0",bannerPage.getTotal());
    }

    // 添加banner
    @PostMapping("addBanner")
    public R addBanner (@RequestBody CrmBanner banner) throws Exception {

        boolean save = service.save(banner);
        if (!save) {
            throw new Exception("添加banner失败!");
        }

        return R.ok();
    }

    // 显示指定banner
    @GetMapping("getBannerById/{id}")
    public R getBannerById (@PathVariable String id){
        CrmBanner banner = service.getById(id);
        return R.ok().data("banner",banner);
    }

    // 修改banner
    @PostMapping("updateBanner")
    public R updateBanner (@RequestBody CrmBanner banner) throws Exception {

        boolean b = service.updateById(banner);
        if (!b){
            throw new Exception("修改banner失败!");
        }
        return R.ok();
    }


    // 删除banner
    @DeleteMapping("deleteBanner/{id}")
    public R deleteBanner(@PathVariable String id) throws Exception {
        boolean b = service.removeById(id);
        if (!b){
            throw new Exception("删除banner失败!");
        }
        return R.ok();
    }

}

